import requests
import json
from Crypto.Cipher import AES
import base64
import time
import pandas as pd
import random
url = "https://music.163.com/weapi/v1/resource/comments/R_SO_4_29004400?csrf_token="

headers = json.loads(r'''{
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Length": "486",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "vjuids=cd247531c.165555b70cf.0.b1a44330616cd; vjlast=1534735381.1536051608.13; vinfo_n_f_l_n3=e52c13c4e02a5f87.1.1.1534141467235.1534141917201.1536051612288; JSESSIONID-WYYY=9QrVda1d6woljKYDRlUi0IgIBa3hJ5Id%2B9Hn8cgHzhg8%2BnmGyToppQef3uB%2BQ7C1PfUgEuiXm0BExhCycPoe0v4nKMByZA%2F%2Fo7CBdNG0toJm1ahXaqEIO6792137Jy2WYwps3KCfbBOlvv8uagsBzYgWXJYigWC896w2gwE5%5C7etpjqU%3A1536133603514; _iuqxldmzr_=32; _ntes_nnid=dc2af2c76d7c7adaff238db8635f0757,1536131803570; _ntes_nuid=dc2af2c76d7c7adaff238db8635f0757; WM_NI=OdhtWeJlbhZ%2BHCQqTmXesQYQi6eIcbbyJ3Uej5YQbMWKkchzfHDdvDiXY922FGW2eqI2NIgyNRulTPqJXOG%2BakfUCLTVajPOZW%2F0z5evXJS0AEwey3ItvKIvGibCeb1UV3k%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeccd87bb18babb7d63f9a8aa5d4f6619af0fa89c55bad9598d1cb428a89bc89f02af0fea7c3b92a8dbca895d66f8ab4ae89d9218c9d9f92d7679cbbbdb2dc5babeaabaae26aa2a786d4d86faaeb97b2e1479595a584eb72a6988c85f550ab8897dab13a869999d3e87b8bee82aae6738e8ebb8dd75cb39698b0c621a3b5aab6c248988aaf8bd3459a89e1d1f93fa7aa98abd23c839bb986fc398bacbaaccc7e85b7aea4e86af38b9ad4ea37e2a3; WM_TID=zrm%2BgZsCkfC7aWaT5nlBjYKbq60Qc3A8",
    "Host": "music.163.com",
    "Origin": "https://music.163.com",
    "Referer": "https://music.163.com/song?id=29004400",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
}''')
forth_param = "0CoJUm6Qyw8W8jud"
encSecKey = "a8ece0acb7c6b5c58db59b3a87cc4163280ed11f0e96f87006daf983d3f4372aff2df5b6e51c352b65646ee547dcb8a4990f70a506530e884a2fa4bd1596ede5b21ad35ea71c9408fdbb3edcd8e634543040d1e967c8b702cb695929b985806cf6c43da29dc802e040b0f041903e84ac8e2ab97dbedcb0070fc74c71b900d201"

def AES_encrypt(text, key, iv):
    pad = 16 - len(text) % 16
    text = text + pad * chr(pad)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    encrypt_text = encryptor.encrypt(text)
    encrypt_text = base64.b64encode(encrypt_text)
    return encrypt_text


def get_params(i):
    if i == 0:
        first_param = '{rid:"", offset:"0", total:"true", limit:"20", csrf_token:""}'
    else:
        offset =str(i*20)
        first_param = '{rid:"", offset:"%s", total:"%s", limit:"20", csrf_token:""}'%(offset,'flase')
    iv = "0102030405060708"
    first_key = forth_param
    second_key = 16 * 'F'
    h_encText = AES_encrypt(first_param, first_key, iv)
    h_encText = AES_encrypt(h_encText, second_key, iv)
    return h_encText

for i in range(0,2):
	params = get_params(i)
	asd = {"params":params,"encSecKey":encSecKey}
	data = json.loads(asd)
	resp = requests.post(url, data=data, headers=headers)
	print(resp.text)
